import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import { AppstoreOutlined, CoffeeOutlined, HomeOutlined, OneToOneOutlined, PicRightOutlined, UserOutlined } from '@ant-design/icons-vue';

export const routes: RouteRecordRaw[] = [
    {
        path: '/admin',
        component: () => import('@/layout/Index.vue'),
        children: [
            {
                path: '/admin',
                component: () => import('@/views/admin/index/Index.vue'),
                name: 'index',
                meta: { title: '管理首页', icon: HomeOutlined, requiresAuth: true }
            },
            {
                path: '/admin/coffee',
                name: 'coffee',
                children: [
                    {
                        path: '/admin/coffee',
                        component: () => import('@/views/admin/coffee/Index.vue'),
                        name: 'coffeeList',
                        meta: { title: '品类列表', requiresAuth: true }
                    },
                    {
                        path: '/admin/coffee/editor/:id(\\d+)',
                        component: () => import('@/views/admin/coffee/Editor.vue'),
                        name: 'coffeeEditor',
                        meta: { title: '品类编辑', hidden: true, requiresAuth: true }
                    },
                    {
                        path: '/admin/coffee/create',
                        component: () => import('@/views/admin/coffee/Create.vue'),
                        name: 'coffeeCreate',
                        meta: { title: '品类创建', hidden: true, requiresAuth: true }
                    },
                ],
                meta: { title: '咖啡品类', icon: CoffeeOutlined, hiddenChildren: true, requiresAuth: true }
            },
            {
                path: '/admin/category',
                component: () => import('@/views/admin/category/Index.vue'),
                name: 'category',
                meta: { title: '分类管理', icon: AppstoreOutlined, requiresAuth: true }
            },
            {
                path: '/admin/templates',
                component: () => import('@/views/admin/templates/Index.vue'),
                name: 'Templates',
                meta: { title: '信息模板', icon: PicRightOutlined, requiresAuth: true }
            },
            {
                path: '/admin/fields',
                component: () => import('@/views/admin/fields/Index.vue'),
                name: 'Fields',
                meta: { title: '字段管理', icon: OneToOneOutlined, requiresAuth: true }
            },
            {
                path: '/admin/user',
                component: () => import('@/views/admin/user/Index.vue'),
                name: 'user',
                meta: { title: '用户管理', icon: UserOutlined, requiresAuth: true }
            }
        ]
    },
    {
        path: '/admin/login',
        name: 'adminLogin',
        component: () => import('@/views/admin/login.vue'),
        meta: { title: '管理后台登录', requiresAuth: false }
    },
    {
        path:'/',
        component: () => import('@/views/home/Layout.vue'),
        meta: { title: '首页' },
        children: [
            {
            path: '/',
            name: 'home',
            component: () => import('@/views/home/Index.vue'),
            meta: { title: '首页', requiresAuth: false }
            },
            {
                path: '/coffee/:id(\\d+)',
                name: 'CoffeeDetail',
                component: () => import('@/views/home/Detail.vue'),
                meta: { title: '品类详情', requiresAuth: false }
            }
        ]
    },
]
   

const router = createRouter({
    routes,
    history: createWebHistory(),
    //路由模式
    

})
const documentTitle = import.meta.env.VITE_APP_TITLE
// 路由守卫
router.beforeEach((to,_from,next)=>{
    document.title = `${to.meta.title} - ${documentTitle}`
    if(to.meta.requiresAuth && !localStorage.getItem('token')){
        next('/admin/login')
    }else{
        next()
    }
    
})
export default router